--[[
	Copyright (C) Udorn (Blackhand)
--]]

local viewType = "ToggleIcon"
local version = 1
local YakaGui = LibStub("YakaGui", true)
local info = YakaGui:GetViewInfo(viewType) 
if (info and info.version >= version) then 
	return 
end

yaka = yaka or {}
yaka.ToggleIcon = {}
yaka.ToggleIcon.prototype = {}
yaka.ToggleIcon.metatable = {__index = yaka.ToggleIcon.prototype}
setmetatable(yaka.ToggleIcon.prototype, {__index = yaka.Icon.prototype})

yaka.ToggleIcon.info = {viewType = viewType, version = version}
yaka.ToggleIcon.GetViewInfo = function()
	return yaka.ToggleIcon.info
end

local log = vendor.Debug:new("ToggleIcon")

local function _UpdateChecked(self)
	local cfg = self.cfg
	if (self.checked) then
		self:SetNormalTexture(cfg.checkedNormalTexture) 
		self:SetPushedTexture(cfg.checkedPushedTexture)
	else
		self:SetNormalTexture(cfg.uncheckedNormalTexture) 
		self:SetPushedTexture(cfg.uncheckedPushedTexture)
	end
end

local function _OnClick(this, button)
	local self = this.view
	self.checked = not self.checked
	if (self.cfg.setValue) then
		local info = {arg = self.cfg.arg, view = self, value = self.checked}
		self.cfg.setValue(info)
	end
	_UpdateChecked(self)
end

function yaka.ToggleIcon:new(cfgIn)
	local instance = setmetatable({}, self.metatable)
	instance:InitCfg(cfgIn)
	instance:InitFrame()
	return instance
end

function yaka.ToggleIcon.prototype:InitCfg(cfgIn)
	yaka.Icon.prototype.InitCfg(self, cfgIn)
	
	local cfg = self.cfg
	cfg.checkedNormalTexture = cfgIn.checkedNormalTexture 
	cfg.checkedPushedTexture = cfgIn.checkedPushedTexture
	cfg.uncheckedNormalTexture = cfgIn.uncheckedNormalTexture 
	cfg.uncheckedPushedTexture = cfgIn.uncheckedPushedTexture
	cfg.setValue = cfgIn.setValue
	cfg.getValue = cfgIn.getValue
	cfg.checked = cfgIn.checked
	cfg.onClick = _OnClick
end

function yaka.ToggleIcon.prototype:InitFrame()
	yaka.Icon.prototype.InitFrame(self)
	
	local cfg = self.cfg
	
	if (cfg.getValue) then
		self.checked = cfg.getValue({view = self, arg = cfg.arg})
	else
		self.checked = cfg.checked
	end
	_UpdateChecked(self)
end

function yaka.ToggleIcon.prototype:SetChecked(checked)
	self.checked = checked
	_UpdateChecked(self)
end

function yaka.ToggleIcon.prototype:GetChecked(checked)
	return self.checked
end

-- register view

YakaGui:RegisterViewType(yaka.ToggleIcon)